package de.velvettear.musiclister.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.HashMap;

public class SettingsHelper {

	public final static String INPUT = "INPUT";
	public final static String COVERS = "COVERS";	
	public final static String HOST = "HOST";
	public final static String USER = "USER";
	public final static String PASSWORD = "PASSWORD";
	public final static String DATABASE = "DATABASE";
	public final static String PREFIX = "PREFIX";
	
	private final static String SETTINGS = "settings.ini";
	
	public static String getCurrentDirectory() {
		String path = "";
		try {
			path = new File(".").getCanonicalPath();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return path;
	}
	
	public static String getInput() {
		HashMap<String, String> settings = readSettings();
		
		return settings.get(INPUT);
	}
	
	public static String getCovers() {
		HashMap<String, String> settings = readSettings();
		
		return settings.get(COVERS);
	}
	
	public static String getHost() {
		HashMap<String, String> settings = readSettings();
		
		return settings.get(HOST);
	}
	
	public static String getUser() {
		HashMap<String, String> settings = readSettings();
		
		return settings.get(USER);
	}
	
	public static String getPassword() {
		HashMap<String, String> settings = readSettings();
		
		return settings.get(PASSWORD);
	}
	
	public static String getDatabase() {
		HashMap<String, String> settings = readSettings();
		
		return settings.get(DATABASE);
	}
	
	public static String getPrefix() {
		HashMap<String, String> settings = readSettings();
		
		return settings.get(PREFIX);
	}
	
	public static HashMap<String, String> readSettings() {
		HashMap<String, String> settings = new HashMap<String, String>();
		String input = "";
		String covers = "";
		String host = "";
		String user = "";
		String password = "";
		String database = "";
		String prefix = "";
				
		File settingsFile = new File(getCurrentDirectory() + File.separator + SETTINGS);
		if (settingsFile.exists()) {
			try { 
				BufferedReader reader = new BufferedReader(new FileReader(settingsFile));		
				
				String tmp = reader.readLine();
				while (tmp != null && tmp.trim().length() > 0) {
					if (tmp.startsWith(INPUT)) {
						input = tmp.split("=")[1];
					} else if (tmp.startsWith(COVERS)) {
						covers = tmp.split("=")[1];
					} else if (tmp.startsWith(HOST)) {
						host = tmp.split("=")[1];
					} else if (tmp.startsWith(USER)) {
						user = tmp.split("=")[1];
					} else if (tmp.startsWith(PASSWORD)) {
						password = tmp.split("=")[1];
					} else if (tmp.startsWith(DATABASE)) {
						database = tmp.split("=")[1];
					} else if (tmp.startsWith(PREFIX)) {
						prefix = tmp.split("=")[1];
					}
					tmp = reader.readLine();
				}				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		settings.put(INPUT, input);
		settings.put(COVERS, covers);
		settings.put(HOST, host);
		settings.put(USER, user);
		settings.put(PASSWORD, password);
		settings.put(DATABASE, database);
		settings.put(PREFIX, prefix);
		
		return settings;
	}
		
	public static void writeSettings(String input, String covers) {
		HashMap<String, String> settings = readSettings();
		String host = settings.get(HOST);
		String user = settings.get(USER);
		String password = settings.get(PASSWORD);
		String database = settings.get(DATABASE);
		String prefix = settings.get(PREFIX);
		
		File settingsFile = new File(getCurrentDirectory() + File.separator + SETTINGS);
		
		if (settingsFile.exists()) {
			settingsFile.delete();
		}
		
		try {
			BufferedWriter writer = new BufferedWriter(new FileWriter(settingsFile));
			writer.append(INPUT + "=" + input + "\n");
			writer.append(COVERS + "=" + covers + "\n");
			writer.append(HOST + "=" + host + "\n");
			writer.append(USER + "=" + user + "\n");
			writer.append(PASSWORD + "=" + password + "\n");
			writer.append(DATABASE + "=" + database + "\n");
			writer.append(PREFIX + "=" + prefix + "\n");
			writer.flush();
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
		}		
	}
	
	public static void writeSettings(String host, String user, String password, String database, String prefix) {
		File settingsFile = new File(getCurrentDirectory() + File.separator + SETTINGS);
		
		if (settingsFile.exists()) {
			settingsFile.delete();
		}
		
		try {
			BufferedWriter writer = new BufferedWriter(new FileWriter(settingsFile));
			writer.append(HOST + "=" + host + "\n");
			writer.append(USER + "=" + user + "\n");
			writer.append(PASSWORD + "=" + password + "\n");
			writer.append(DATABASE + "=" + database + "\n");
			writer.append(PREFIX + "=" + prefix + "\n");
			writer.flush();
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
		}		
	}
	
}
